//
//  FindCompanyViewController.m
//  YaoHeAPP
//
//  Created by xyh on 16/7/5.
//  Copyright © 2016年 mac. All rights reserved.
//

#import "FindCompanyViewController.h"
#import "Masonry.h"
#import "Controller.h"
#import "QueryCompanyTableView.h"
#import "TongYong.h"
#import "CompanyItemModel.h"
#import "MJExtension.h"
#import "labelview.h"
#import "CreateCompanyVC.h"

@interface FindCompanyViewController ()<UISearchBarDelegate,UIAlertViewDelegate,labelviewdelegate>
{
    NSInteger width;
    NSInteger height;
    labelview *la;//无数据显示的页面
}
@property(nonatomic,strong) QueryCompanyTableView *queryCompanyTableView;
@property(nonatomic,strong) NSArray *companyItemArray;
@property (strong, nonatomic)  UITableView *tableView;
@property (strong, nonatomic)  UISearchBar *searchBar;
@property (strong, nonatomic)  CompanyItemModel *companyItemModelData;//确认点击回来的数据

@end

@implementation FindCompanyViewController
- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [MobClick beginLogPageView:@"RegistSuccess"];
}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    [MobClick endLogPageView:@"RegistSuccess"];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    //调一个背景色
    UIColor*backColor=[UIColor whiteColor];
    UIColor*alphColor=[backColor colorWithAlphaComponent:0.96];
    self.view.backgroundColor=alphColor;
    
    width=self.view.frame.size.width;
    height=self.view.frame.size.height;
    self.view.backgroundColor=bgVCcolor;
    //加一个导航条
    UIView*navcBar=[[UIView alloc]init];
    navcBar.backgroundColor=[UIColor colorWithRed:90.f/255.f green:191.f/255.f blue:219.f/255.f alpha:1];
    [self.view addSubview:navcBar];
    [self.view bringSubviewToFront:navcBar];
    [navcBar mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.offset(0);   make.left.offset(0);
        make.height.offset(100);  make.right.offset(0);
    }];
    //导航条标题
    UILabel*titleLabel=[[UILabel alloc]init];
    titleLabel.text=@"查找公司";
    titleLabel.font=Font(17);
    titleLabel.textAlignment=NSTextAlignmentCenter;
    titleLabel.textColor=[UIColor whiteColor];
    [navcBar addSubview:titleLabel];
    [titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.offset(25);
        make.left.offset(width/2-50);
        make.width.offset(100);
        make.height.offset(30);
    }];
    
    //返回按钮
    UIButton*leftButton=[UIButton buttonWithType:UIButtonTypeCustom];
    [leftButton setBackgroundImage:[UIImage imageNamed:@"return"] forState:UIControlStateNormal];
    [leftButton addTarget:self action:@selector(backLogin) forControlEvents:UIControlEventTouchUpInside];
    [navcBar addSubview:leftButton];
    [leftButton mas_makeConstraints:^(MASConstraintMaker*make) {
        make.left.offset(6);
        make.width.offset(24);
        make.height.offset(22);
        make.top.offset(31);
    }];
    
    
    _searchBar = [[UISearchBar alloc]init ];
    _searchBar.placeholder = @"按公司名称查找";
    _searchBar.delegate=self;
    _searchBar.barTintColor=[UIColor colorWithRed:90.f/255.f green:191.f/255.f blue:219.f/255.f alpha:1];
    _searchBar.layer.borderWidth = 1;
    _searchBar.layer.borderColor=[RGBCOLOR(90, 191, 219) CGColor];
    _searchBar.returnKeyType = UIReturnKeyDefault;
    [navcBar addSubview:_searchBar];
    [_searchBar mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.offset(60);
        make.left.offset(10);
        make.height.offset(40);
        make.right.offset(-40);
    }];
    
    UIButton*rightButton=[UIButton buttonWithType:UIButtonTypeCustom];
    [rightButton setTitle:@"搜索" forState:UIControlStateNormal];
    rightButton.titleLabel.font=Font(14);
    [rightButton addTarget:self action:@selector(keyboardDissmiss) forControlEvents:UIControlEventTouchUpInside];
    [navcBar addSubview:rightButton];
    [rightButton mas_makeConstraints:^(MASConstraintMaker*make) {
        make.width.offset(30);
        make.right.offset(-5);
        make.height.offset(30);
        make.top.offset(70);
    }];

}

#pragma makr - UISearchBarDelegate
- (BOOL)searchBarShouldBeginEditing:(UISearchBar *)searchBar{
    return YES;
}

- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText{
    if (![TongYong isBlankObjectAndStr:searchText]) {
        //调取查找相关公司的名称 //进行查询
        [self queryCopmany];
    }else{
        [_queryCompanyTableView removeFromSuperview];
    }
}

//公司名称模糊查询
-(void)queryCopmany
{
    NSDictionary*dic=@{@"COMPANY_NAME":_searchBar.text};
    [Controller requestWithURL:PostlikelyCompanyName jsondic:dic finishBlock:^(ClassData *classData) {
        if ([classData.number integerValue]==0) {
            _companyItemArray = [CompanyItemModel mj_objectArrayWithKeyValuesArray:classData.data];
            
            if (_companyItemArray.count==0) {
                
                 [_queryCompanyTableView removeFromSuperview];
                
                    if (la) {
                        [la removeFromSuperview];
                    }
                    la=[[labelview alloc]init];
                    [la setimage:@"Nodata_wenhao" tit:@"暂无相关公司" buttontit:@"创建公司" frem:CGRectMake(0, 120, self.view.frame.size.width, self.view.frame.size.height)];
                    la.backgroundColor= [UIColor colorWithRed:230/255.0 green:232/255.0 blue:241/255.0  alpha:1];
                    la.image.frame=CGRectMake((self.view.frame.size.width-100)/2, 50, 100, 125);
                    la.label.frame=CGRectMake(50, 180, self.view.frame.size.width-100, 40);
                    la.button.frame=CGRectMake(40, 230, self.view.frame.size.width-80, 40);
                    la.delegate=self;
                    [self.view addSubview:la];

            }else{
            
                if (la) {
                    [la removeFromSuperview];
                }
                
                [self.view addSubview:self.queryCompanyTableView];
                self.queryCompanyTableView.QueryCompanyList =_companyItemArray;
                [self.queryCompanyTableView reloadData];
            
            }
            
        }
        else{
            ALERTSHOW(classData.message);
        }
    }];
    
}

- (QueryCompanyTableView *)queryCompanyTableView {
    
    if (!_queryCompanyTableView) {
        _queryCompanyTableView = [[QueryCompanyTableView alloc] initWithFrame:CGRectMake(0, 110, SCREEN_WIDTH, 350) style:UITableViewStylePlain ];
        __block FindCompanyViewController *weakSelf = self;
        _queryCompanyTableView.cancelActionBlock = ^() {
            [weakSelf closeIt];
        };
        
        _queryCompanyTableView.didSelectRowBlock = ^(NSInteger indexPathRow) {
            weakSelf.companyItemModelData = weakSelf.companyItemArray[indexPathRow];
            NSString *mesagest=[NSString stringWithFormat:@"%@",weakSelf.companyItemModelData.COMPANY_NAME];
            weakSelf.searchBar.text=mesagest;
            UIAlertView *alertViewCT=[[UIAlertView alloc]initWithTitle:@"加入公司" message:mesagest delegate:weakSelf cancelButtonTitle:@"取消" otherButtonTitles:@"加入", nil];
            alertViewCT.tag=20;
            [alertViewCT show];
            
            [weakSelf closeIt];
            
        };
    }
    return _queryCompanyTableView;
}

-(void)closeIt
{
    [_queryCompanyTableView removeFromSuperview];
    
}

-(void)keyboardDissmiss{
    //重新搜索
    [self queryCopmany];
}

-(void)backLogin{
    
    [self.navigationController popViewControllerAnimated:YES];
    
}
//警告框的协议方法 下方两个按钮的索引
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (alertView.tag==20) {
        if (buttonIndex==0) {
            //取消
        }else if (buttonIndex==1){
           //加入公司
            NSDictionary*dic=@{@"MEMBER_ID":_companyItemModelData.COMPANY_ID};
            [Controller requestWithURL:PostApplyForCompany jsondic:dic finishBlock:^(ClassData *classData) {
                if ([classData.number integerValue]==0) {
                    
                    ALERTSHOW(@"您已成功提交申请");
                    
                }else{
                    ALERTSHOW(classData.message);
                }
            }];

            
        
        }
    }
}

//创建公司按钮
- (void)didClickButtonInBottomView{
     //点击了按钮
    CreateCompanyVC *creatVC=[CreateCompanyVC new];
    [self.navigationController pushViewController:creatVC animated:YES];
    
}

@end
